package com.hyt.it.ogt.common.utils;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {
	
	public static final int DAY_SECONDS = 24 * 60 * 60; 
    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public static final String YYYY_MM_DD = "yyyy-MM-dd";
    public static final String MM_DD_HH_MM_SS = "MM-dd HH:mm:ss";
	public static final String YYYYMMDDHHMMSS = "yyyymmddhhmmss";

	/**
	 * 获取当前时间前一小时的时间
	 * @param date
	 * @return java.util.Date
	 */
	public static Date beforeOneHourToNowDate(Date date) {
	    Calendar calendar = Calendar.getInstance();
	    /* HOUR_OF_DAY 指示一天中的小时 */
	    calendar.setTime(date);
	    calendar.add(Calendar.HOUR_OF_DAY, -1);
	    return calendar.getTime();
	}
	
	/**
	 * 获取当前时间后n小时的时间
	 * @param date
	 * @return java.util.Date
	 */
	public static Date afterHoursToNowDate(Date date, int hours) {
		if(hours < 0) {
			hours = 1;
		}
	    Calendar calendar = Calendar.getInstance();
	    /* HOUR_OF_DAY 指示一天中的小时 */
	    calendar.setTime(date);
	    calendar.add(Calendar.HOUR_OF_DAY, hours);
	    return calendar.getTime();
	}
	
	public static Date asDate(LocalDate localDate) {
        return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
    }

    public static Date asDate(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static LocalDate asLocalDate(Date date) {
        return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
    }

    public static LocalDateTime asLocalDateTime(Date date) {
        return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
    }
    
    /**
     * <p>
     * 鏃ユ湡鏃堕棿鏍煎紡yyyy-MM-dd HH:mm:ss
     *
     * @return
     */
    public static String currentDatetime() {
        SimpleDateFormat datetimeFormat = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        return datetimeFormat.format(today());
    }


	/**
	 * 获取当前系统时间
	 * @param format
	 * @return
	 */
	public static String currentDatetime(String format) {
		SimpleDateFormat datetimeFormat = new SimpleDateFormat(format);
		return datetimeFormat.format(today());
	}
    /**
     *
     * @return Date
     */
    public static Date today() {
        return new Date();
    }
}
